package demo;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import okio.BufferedSink;
import okio.Okio;
import okio.Sink;

public class SinkDemo1 {

	public static void main(String[] args) {
		String filename = "E:\\zsl\\okio2.txt";
        boolean isCreate = false;
        Sink sink;
        BufferedSink bSink = null;
        try {
            //判断文件是否存在，不存在，则新建！
            File file = new File(filename);
            if (!file.exists()) {
                isCreate = file.createNewFile();
            } else {
                isCreate = true;
            }
            //写入操作
            if (isCreate) {
                sink = Okio.sink(file);
                bSink = Okio.buffer(sink);
                bSink.writeUtf8("1").writeUtf8("\n").writeUtf8("this is new file!").writeUtf8("\n").writeUtf8("张苏磊！").writeUtf8("\n");
                StringBuilder s = new StringBuilder("");
                for(int i=0;i<100000;i++){
                	s.append("this is new file!");
                }
                bSink.writeUtf8(s.toString());
                bSink.writeString("我是每二条", Charset.forName("utf-8"));
                bSink.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bSink) {
                    bSink.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}

}
